package com.sskj.video.my;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sskj.video.ArtemisPostUtil;
import com.sskj.video.CameraVO;
import com.sskj.video.HttpResultResponse;
import com.sskj.video.model.EventModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Like
 * @Date: 2024/3/4 15:41
 * @Description:
 */

@Slf4j
@RestController
@RequestMapping("/hik")
public class HikController {

    @PostMapping("/callPostV1Cameras")
    public HttpResultResponse callPostV1Cameras(){
        log.info("TestController：request /callPostV1Cameras : 开始运行");
        try {
//            String callPostV1Cameras = ArtemisPostUtil.callPostV1Cameras();
            String callPostV1Cameras = HikUtil.callPostV1Cameras();
            JSONObject jsonObject01 = JSON.parseObject(callPostV1Cameras);
            JSONObject jsonObject02 = JSON.parseObject(String.valueOf(jsonObject01.get("data")));
            JSONArray jsonArray = JSONArray.parseArray(String.valueOf(jsonObject02.get("list")));
            ArrayList<Object> list = new ArrayList<>();
            for(int i = 0;i<jsonArray.size();i++){
                JSONObject jsonObject03 = JSON.parseObject(String.valueOf(jsonArray.get(i)));
                String cameraIndexCode = (String) jsonObject03.get("cameraIndexCode");
                String VechicleDataResult = HikUtil.callPostPreviewURLs(cameraIndexCode,"hls");
//                String videoInfo = HikUtil.callPostIndexCode(cameraIndexCode);
//                String calledPostTalkURLs =HikUtil.callPostTalkURLs(cameraIndexCode);
                JSONObject jsonObject04 = JSON.parseObject(String.valueOf(VechicleDataResult));
                CameraVO cameraVO = CameraVO.builder()
                        .name(jsonObject03.get("cameraName"))
                    .url(jsonObject04.get("data"))
//                    .channelNo(jsonObject03.get("channelNo"))
                        .cameraIndexCode(jsonObject03.get("cameraIndexCode"))
                        .build();
                list.add(cameraVO);
                System.out.println(cameraVO);
//            System.out.println(calledPostTalkURLs);
            }
            System.out.println("总计："+list.size()+"条");
            return HttpResultResponse.success(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @PostMapping("/callPostV1Cameras1")
    public HttpResultResponse callPostV1Cameras1(){
        log.info("TestController：request /callPostV1Cameras : 开始运行");
        try {
            String V1CamerasResult = ArtemisPostUtil.callPostV1Cameras();
            System.out.println(V1CamerasResult);
            return HttpResultResponse.success(V1CamerasResult);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @PostMapping("/callPostV1Cameras2")
    public HttpResultResponse callPostV1Cameras2(){
        log.info("TestController：request /callPostV1Cameras : 开始运行");
        try {
            String callPostV1Cameras = HikUtil.callPostV1Cameras();
//            System.out.println(callPostV1Cameras);
            JSONObject jsonObject01 = JSON.parseObject(callPostV1Cameras);
            JSONObject jsonObject02 = JSON.parseObject(String.valueOf(jsonObject01.get("data")));
            List<AccessHikCamera> list = JSONArray.parseArray(String.valueOf(jsonObject02.get("list")), AccessHikCamera.class);
            return HttpResultResponse.success(list.toString());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }








//    @PostMapping()
//    public HttpResultResponse callPostV1CamerasDetails(@RequestBody EventModel model){
//        log.info("TestController：request /eventRcv : {}",model.toString());
//
//        return HttpResultResponse.success();
//    }
//
//    @PostMapping()
//    public HttpResultResponse callPostPreviewURLs(@RequestBody EventModel model){
//        log.info("TestController：request /eventRcv : {}",model.toString());
//
//        return HttpResultResponse.success();
//    }
}
